﻿Imports System.Data.SqlClient
Imports System.Data.Odbc

Public Class frmCatalogoProductos
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoProductos_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo de Productos")
        Usuario.Ubicacion("Ventana Principal")
    End Sub

    Private Sub frmCatalogoProductos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgBombas.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al catálogo de Productos")
        Usuario.Ubicacion("Catálogo de Productos")

        CargaProductos()
    End Sub
    Sub CargaProductos()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBombas.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select *" & _
                     "from Productos" & _
                     " WHERE UPPER(CodigoProducto) LIKE '" & Filtro & "' " & mstrCondicion & _
                     " OR UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion & _
                     " OR UPPER(Familia) like '" & Filtro & "' " & mstrCondicion & _
                     " OR UPPER(SubFamilia) like '" & Filtro & "' " & mstrCondicion


            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBombas.Rows.Add(rdBuscar("CodigoProducto").ToString.Trim, rdBuscar("Nombre").ToString.Trim, rdBuscar("Familia").ToString.Trim, rdBuscar("SubFamilia").ToString.Trim)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaProductos()
    End Sub
    Sub Importar()
        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        cnConn.ConnectionString = CitraConnection

        strSql = "SELECT cCodigoP01,cNombreP01,cTextoEx01,cTextoEx02,cDescrip01,cIdValor01,cIdValor02,cStatusP01 FROM MGW10005 WHERE cTipoPro01=1"


        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql


        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim Prod As New clsProductos
        Dim IdProducto As Integer

        Do While rdBuscar.Read()

            Prod.CodigoProducto = rdBuscar("cCodigoP01").ToString.Trim
            Prod.Nombre = rdBuscar("cNombreP01").ToString.Trim
            Prod.NombreMecanico = rdBuscar("cTextoEx01").ToString.Trim
            Prod.NombreProveedor = rdBuscar("cTextoEx02").ToString.Trim
            Prod.NombreLargo = ""
            Prod.Descripcion = rdBuscar("cDescrip01").ToString.Trim
            Prod.Status = rdBuscar("cStatusP01")
            Prod.Familia = BuscarStrDatoCitra("MGW10020", "cValorCL01", "cIdValor01=" & rdBuscar("cIdValor01"))
            Prod.SubFamilia = BuscarStrDatoCitra("MGW10020", "cValorCL01", "cIdValor01=" & rdBuscar("cIdValor02"))
            IdProducto = BuscarIntDatoCitra("Productos", "IdProducto", "CodigoProducto='" & rdBuscar("cCodigoP01").ToString.Trim & "'")
            If IdProducto = 0 Then
                Prod.Insertar()
            Else
                Prod.Actualizar()
            End If
        Loop
        CargaProductos()
    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Importar()

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
End Class